<?php

include_once 'professor.php';

class professorManager
{
    public static function addProfessor($professor)
    {
        $professor = mysql_real_escape_string($professor);
        $name = $professor->getName();
        $id = $professor->getID();
        $location = $professor->getLocation();
        $user = $professor->getUserID();
        
        $insert = mysql_query("INSERT INTO professor VALUES(NULL, '$name', '$location', '$user')");
        $num = mysql_affected_rows();
        if ($num == 1)
        {
            echo "The professor has been successfully added.";
            $select = mysql_query("SELECT * FROM professor ORDER BY ProfessorID DESC LIMIT 1");
            $result = mysql_fetch_array($select);
            $professor->setID($result['ProfessorID']);
            return $professor;
        }
        else
            echo "The professor has not been successfully added.";
        echo "<br>";
    }
    
    public static function editProfessor($professor)
    {
        $professor = mysql_real_escape_string($professor);
        $name = $professor->getName();
        $id = $professor->getID();
        $location = $professor->getLocation();
        
        $update = mysql_query("UPDATE professor SET Name = '$name', Location = '$location' WHERE ProfessorID = '$id' LIMIT 1");
        $num = mysql_affected_rows();
        if ($num == 1)
            echo "The professor has been successfully updated.";
        else
            echo "The professor has not been successfully updated.";
        echo "<br>";
    }
    
    public static function getProfessor($professorID)
    {
        $professorID = mysql_real_escape_string($professorID);
        $select = mysql_query("SELECT * FROM professor WHERE ProfessorID = '$professorID'") or die(mysql_error());
        $result = mysql_num_rows($select);
        if ($result > 0)
        {
            $profInfo = mysql_fetch_array($select);
            $professor = new Professor();
            $professor->setID($profInfo['ProfessorID']);
            $professor->setName($profInfo['Name']);
            $professor->setLocation($profInfo['Location']);
            return $professor;
        } else
            echo "There is no professor with that ID.";
    }
        
    public static function getProfessors($userID)
    {
        $userID = mysql_real_escape_string($userID);
        $select = mysql_query("SELECT * FROM professor WHERE UserID = '$userID'") or die(mysql_error());
        $profArray = array();
        while($row = mysql_fetch_array($select))
        {
            $prof = new Professor();
            $prof = professorManager::getProfessor($row['ProfessorID']);

            $profArray[] = $prof;
        }
        return $profArray;
    }
    
    public static function deleteProfessor($professor)
    {
        $professor = mysql_real_escape_string($professor);
        $ID = $professor->getID();
        $delete = mysql_query("DELETE FROM professor WHERE ProfessorID = '$ID' LIMIT 1");
        $num = mysql_affected_rows();
        if ($num == 1)
            echo "The professor has been successfully deleted.";
        else
            echo "The professor has not been successfully deleted.";
        echo "<br>";
    }
}

?>
